package listbyorder.access101_200.test120;

import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/21 10:49
 */
public class Solution4 {
    // 动态规划，正向遍历
    public int minimumTotal(List<List<Integer>> triangle) {
        int size = triangle.size();
        int min = Integer.MAX_VALUE;
        int[][] dp = new int[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j <= i; j++) {
                if (i == 0) {
                    dp[i][j] = triangle.get(i).get(j);
                } else if (j == 0) {
                    dp[i][j] = dp[i - 1][j] + triangle.get(i).get(j);
                } else if (j == i) {
                    dp[i][j] = dp[i - 1][j - 1] + triangle.get(i).get(j);
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j]) + triangle.get(i).get(j);
                }
            }
        }
        for (int i = 0; i < size; i++) {
            if (min > dp[size - 1][i]) {
                min = dp[size - 1][i];
            }
        }
        return min;
    }

}
